// src/main/java/com/bookmanager/controller/RegisterController.java
package com.bookmanager.controller;

import com.bookmanager.model.User;
import com.bookmanager.service.UserService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/register")
public class RegisterController extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 显示注册页面
        request.getRequestDispatcher("register.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取注册参数
        String userId = request.getParameter("userId");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String role = request.getParameter("role");

        System.out.println("🔍 接收到注册请求: userId=" + userId + ", username=" + username + ", role=" + role);

        // 基本验证
        if (userId == null || userId.trim().isEmpty()) {
            request.setAttribute("error", "用户ID不能为空");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        if (password == null || !password.equals(confirmPassword)) {
            request.setAttribute("error", "两次输入的密码不一致");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        if (password.length() < 6) {
            request.setAttribute("error", "密码长度至少6位");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        // 创建用户对象
        User user = new User();
        user.setUserId(userId.trim());
        user.setUsername(username != null ? username.trim() : "");
        user.setPassword(password);
        user.setEmail(email != null ? email.trim() : "");
        user.setPhone(phone != null ? phone.trim() : "");
        user.setRole(role != null ? role : "reader"); // 默认普通用户

        try {
            // 调用注册服务
            String result = userService.register(user, confirmPassword);

            if ("success".equals(result)) {
                System.out.println("✅ 注册成功: " + userId);
                request.setAttribute("success", "注册成功！请使用新账号登录");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            } else {
                System.out.println("❌ 注册失败: " + result);
                request.setAttribute("error", result);
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        } catch (Exception e) {
            System.out.println("❌ 注册过程出错: " + e.getMessage());
            e.printStackTrace();
            request.setAttribute("error", "系统错误，请稍后重试");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }
}
